package cc.market.service.impl;

import cc.market.entity.ProductInfo;
import cc.market.mapper.ProductMapper;
import cc.market.entity.ProductCategory;
import cc.market.service.ProductService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ProductServiceImpl implements ProductService {


    @Autowired
    private ProductMapper productMapper;


    /**
     * 获取产品分类
     * @return
     */
    @Override
    public List<ProductCategory> get() {
        return productMapper.getCategory();
    }

    /**
     * 商品上传功能
     * @param productInfo
     */
    @Override
    public void insert(ProductInfo productInfo) {
        productInfo.setCreateTime(LocalDateTime.now());
        productInfo.setUpdateTime(LocalDateTime.now());
        log.info("此时的实体类：{}",productInfo);
        productMapper.insert(productInfo);
    }

    /**
     * 根据分类查询商品
     * @param categoryId
     * @return
     */
    @Override
    public List<ProductInfo> getByCategoryId(Integer categoryId) {
        if(categoryId == null){
            /* 传过来的数据为空就是查询全部 */
            return productMapper.getAll();
        }else {
            /* 按照id查询*/
            return productMapper.getByCatrgoryId(categoryId);
        }

    }

    /**
     * 根据定位查询商品
     * @param province
     * @param city
     * @return
     */
    @Override
    public List<ProductInfo> getByLocation(String province, String city) {
        return productMapper.getByLocation(province , city);
    }

    @Override
    public ProductInfo getById(Integer id) {
        return productMapper.getById(id);
    }
}
